<?php
    include APPLICATION_PATH . '/application/views/head.phtml';
    include APPLICATION_PATH . '/application/views/top.phtml';
    include APPLICATION_PATH . '/application/views/left.phtml';
?>
<script src="/public/js/jquery-migrate-1.2.1.min.js" type="text/javascript"></script>
<script>
$(function() {
    $.ajax({
        type: "post",
        contentType: "application/json",
        url: "/Source/source",
        data: "{}",
        success: function(result) {
            var str = '';
            str += '<option value="--请选择采集的类型--" >--请选择采集的类型--</option>';
            for (var i = 0; i < result.length; i++) {
                str += '<option value="' + result[i]['article_type_id'] + '" >' + result[i]['name'] + '</option>';
            }

            $('#article_typical,#update_article_typical').append(str);
        }
    });
    $('#article_typical,#update_article_typical').change(function() {

        $.ajax({
            type: "post",

            url: "/Source/robot",

            data: {
                "article_type_id": $(this).val()
            },
            dataType : 'JSON',
            success: function(result) {
                $('#source option,#update_source option').remove();
                var str = '';
              str += '<option value="--请选择采集的网站--" >--请选择采集的网站--</option>';
                for (var i = 0; i < result.length; i++) {
                   // alert(result[i]['website_id']);
                    //var category = result[i].split(",");
                    str += '<option value="' + result[i]['website_id'] + '-' + result[i]['article_type_id'] +'"  >' + result[i]['website_name'] + '</option>';
                }
                $('#source,#update_source').append(str);
            },
            error: 　 function(err) {
                layer.alert('error');
                console.log(err);
            }
        });
    });

    $('#source,#update_source').change(function() {
        var arr =$(this).val();
        var website_info = arr.split("-");
        $.ajax({
            type: "post",

            url: "/Source/typical",

            data: {
                "website_id": website_info[0],
                "article_type_id": website_info[1]
            },
            dataType : 'JSON',
            success: function(result) {

                $('#coll_robot option,#update_coll_robot option').remove();
                var str = '';
                str += '<option value="--请选择采集的机器人--" >--请选择采集的机器人--</option>';
               //  alert(result.length);
                for (var i = 0; i < result.length; i++) {

                    str += '<option value="' + result[i]['robot_id'] + '" >' + result[i]['robot'] + '</option>';
                }
                $('#coll_robot,#update_coll_robot ').append(str);
            },
            error: 　 function(err) {
                layer.alert('error');
                console.log(err);
            }
        });
    });
    $('#coll_robot,#update_coll_robot').change(function() {
        $.ajax({
            type: "post",

            url: "/Source/url",

            data: {
                "robot_id": $(this).val(),
            },
            dataType : 'JSON',
            success: function(result) {
                var str = '';
                for (var i = 0; i < result.length; i++) {
                   var name= result[i]['chanel_website'];
                }
                document.getElementById("coll_address").value = name;
                document.getElementById("update_address").value = name;
                
            },
            error: 　 function(err) {
                layer.alert('error');
                console.log(err);
            }
        });
    });
});

$(document).ready(function() {

    var __addForm = $('#form_add'),
        __editForm = $('#form_edit');
    $('#submit_form').click(function() {
       // $('#source,#coll_robot').css("color","red");
        var objForm = {
            __formName: $("input[name='name']", __addForm),
            __formSource: $("select[name='source']", __addForm),
            __formArticleType: $("select[name='article_type_id']", __addForm),
            __formAddress: $("input[name='coll_address']", __addForm),
            __formRobot: $("select[name='coll_robot']", __addForm),
            __formRemark: $("textarea[name='remark']", __addForm)
        }
        for (var i in objForm) {
            if (objForm[i].val() == ''  ) {
                objForm[i].css({
                    'border-color': 'red'
                });
                return false;
            }
             else if(objForm['__formSource'].val() == '--请选择采集的网站--' ){
                objForm['__formSource'].css({
                    'border-color': 'red'
                });
                layer.alert('请选择采集的网站');
                return false;
            }
            
             else {
                objForm[i].css({
                    'border-color': '#e5e5e5'
                });
            }
        }

        $.ajax({
            type: "POST",
            url: "/Source/add",
            data: {
                "name": objForm.__formName.val(),
                "website_id": 　objForm.__formSource.val(),
                "article_type_id": 　objForm.__formArticleType.val(),
                "robot": objForm.__formRobot.val(),
                "url": objForm.__formAddress.val(),
                "remark": 　objForm.__formRemark.val()
            },
            success: function(data) {

                layer.alert(data.info, function() {
                    if (true === data.status) window.location = "/Source/list";
                });


            }
        });

    });

    //$('.itemDelete').click(function(){
    $(document).on('click', '.tableDelete', function() {
        var userId = $(this).parent('td').attr('item_id'),
            thisUrl = $(this).attr('href');
        //官网欢迎页   
        layer.confirm('确认删除', {
            btn: ['确认', '取消'] //按钮
        }, function(index) {

            $.ajax({
                type: "GET",
                url: thisUrl,
                async: true,
                data: {
                    //"id" : userId
                },
                success: function(data) {
                    if (true === data.status) {
                        layer.msg('删除成功！');
                        window.location = "/Source/list";
                    }
                }
            });
            layer.close(index);
            return false;

        }, function(index) {
            layer.close(index);
        });
        return false;

    });


    //$('.itemEdit').click(function(e){
    $(document).on('click', '.tableEdit', function(e) {
        $('#form_edit').modal("toggle");
        item_id = $(this).parent('td').attr('item_id');
        current_status = $(this).parent('td').attr('status');
        current_type = $(this).parent('td').attr('article_type_id');
        current_robot = $(this).parent('td').attr('robot_id');
        current_site = $(this).parent('td').attr('site_id');

        var __parent = $(this).parent('td').parent('tr.gradeX'),
            __formName = $("input[name='name']", __editForm),
            __formSource = $("select[name='source']", __editForm),
            __formAddress = $("input[name='coll_address']", __editForm),
            __formArticleType = $("select[name='article_type_id']", __editForm),
            __formRobot = $("select[name='robot_id']", __editForm),
            __formRemark = $("textarea[name='remark']", __editForm);
        var tdTable = [];
        $('td', __parent).each(function() {
            tdTable.push($(this).text());
        });
       // alert(tdTable);
        __formName.val(tdTable[0]);
        __formSource.val(current_site);
        __formAddress.val(tdTable[3]);
        __formArticleType.val(current_type);
        __formRobot.val(current_robot);
        __formRemark.val(tdTable[5]);

        return false;
    });


    $('#edit_form').click(function() {
        var objForm = {
            __formName: $("input[name='name']", __editForm),
            __formSource: $("select[name='source']", __editForm),
            __formAddress: $("input[name='coll_address']", __editForm),
            __formArticleType: $("select[name='article_type_id']", __editForm),
            __formRobot: $("select[name='robot_id']", __editForm),
            __formRemark: $("textarea[name='remark']", __editForm)
        }

        if ('' == objForm.__formName.val()) {
            window.alert('采集源名称不能为空！');
            return false;
        }

        /*if ('' == objForm.__formSource.val()) {
            window.alert('采集源不能为空！');
            return false;
        }*/

        $.ajax({
            type: "POST",
            url: "/Source/edit/id/" + item_id,
            data: {
                "name": objForm.__formName.val(),
                "source": 　objForm.__formSource.val(),
                "article_type_id": 　objForm.__formArticleType.val(),
                "robot_id": objForm.__formRobot.val(),
                "url": objForm.__formAddress.val(),
                "remark": 　objForm.__formRemark.val()
            },
            success: function(data) {
                layer.alert(data.info, function() {
                    if (true === data.status) window.location = "/Source/list";
                });
            }
        });

    })

    $(document).on('click', '#page_next,#page_prev,#page_index,#page_end', function() {
        var urlOld = $(this).attr('href'),
            urlArr = urlOld.split("/");
        urlId = urlArr[urlArr.length - 1];
        $.ajax({
            type: 'POST',
            url: '/Source/page/page/' + urlId,
            data: urlId,
            success: function(data) {
                var data = $.parseJSON(data);

                $('#tbody').html('');
                var id = '';

                $.each(data, function(k, v) {
                    //console.log('k='+k);
                    id = v['source_id'];
                    article_type_id = v['article_type_id'];
                    robot_id = v['robot_id'];
                    site_id = v['site_id'];

                    $('#tbody').append($('<tr class="gradeX"><td>' + v['name'] + '</td><td>' + v['website_name'] + '</td><td>' + v['typical_name'] + '</td><td>' + v['url'] + '</td><td>' + v['robot_name'] + '</td><td>' + v['remark'] + '</td><td>' + v['current_time'] + '</td><td item_id="' + v['source_id'] + '" article_type_id="' + article_type_id + '"  robot_id="' + robot_id + '" site_id="' + site_id + '"><a class="tableDelete" href="/Source/del/id/' + id + '">删除</a> / <a class="tableEdit" href="/Source/edit/id/' + id + '">修改</a> </td></tr>'));
                });
            }

        });
        $.ajax({
            type: 'POST',
            url: '/Source/currentPage/page/' + urlId,
            data: urlId,
            success: function(data) {
                //console.log(data);
                $('#pages').html(data);
                $('#pages').ready(function() {
                    nextBtn = $('#page_next');
                });
            }

        });
        return false;
    });

});
</script>
 <div id="thispage" data-id="42"></div>
       <!-- BEGIN PAGE -->

        <div class="page-content">

            <!-- BEGIN SAMPLE PORTLET CONFIGURATION MODAL FORM-->

            <div id="portlet-config" class="modal hide">

                <div class="modal-header">

                    <button data-dismiss="modal" class="close" type="button"></button>

                    <h3>portlet Settings</h3>

                </div>

                <div class="modal-body">

                    <p>Here will be a configuration form</p>

                </div>

            </div>

            <!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->

            <!-- BEGIN PAGE CONTAINER-->        

            <div class="container-fluid">

                <!-- BEGIN PAGE HEADER-->

                <div class="row-fluid">

                    <div class="span12">

                        <!-- BEGIN STYLE CUSTOMIZER -->

                        <div class="color-panel hidden-phone">

                            <!-- <div class="color-mode-icons icon-color"></div> -->

                            <div class="color-mode-icons icon-color-close"></div>

                            <div class="color-mode">

                                <p>THEME COLOR</p>

                                <ul class="inline">

                                    <li class="color-black current color-default" data-style="default"></li>

                                    <li class="color-blue" data-style="blue"></li>

                                    <li class="color-brown" data-style="brown"></li>

                                    <li class="color-purple" data-style="purple"></li>

                                    <li class="color-grey" data-style="grey"></li>

                                    <li class="color-white color-light" data-style="light"></li>

                                </ul>

                                <label>

                                    <span>Layout</span>

                                    <select class="layout-option m-wrap small">

                                        <option value="fluid" selected>Fluid</option>

                                        <option value="boxed">Boxed</option>

                                    </select>

                                </label>

                                <label>

                                    <span>Header</span>

                                    <select class="header-option m-wrap small">

                                        <option value="fixed" selected>Fixed</option>

                                        <option value="default">Default</option>

                                    </select>

                                </label>

                                <label>

                                    <span>Sidebar</span>

                                    <select class="sidebar-option m-wrap small">

                                        <option value="fixed">Fixed</option>

                                        <option value="default" selected>Default</option>

                                    </select>

                                </label>

                                <label>

                                    <span>Footer</span>

                                    <select class="footer-option m-wrap small">

                                        <option value="fixed">Fixed</option>

                                        <option value="default" selected>Default</option>

                                    </select>

                                </label>

                            </div>

                        </div>

                        <!-- END BEGIN STYLE CUSTOMIZER -->  

                        <!-- BEGIN PAGE TITLE & BREADCRUMB-->

                        <h3 class="page-title">

                            采集源列表

                        </h3>

                        <ul class="breadcrumb">

                            <li>

                                <i class="icon-home"></i>
                                <span>当前位置：</span>
                                <span>首页</span>
                                <i class="icon-angle-right"></i>

                            </li>

                            <li>

                                <span>采集管理</span>

                                <i class="icon-angle-right"></i>

                            </li>

                            <li><span>采集源列表</span></li>

                        </ul>

                        <!-- END PAGE TITLE & BREADCRUMB-->

                    </div>

                </div>

                <!-- END PAGE HEADER-->

                <!-- BEGIN PAGE CONTENT-->

                <div class="row-fluid">

                    <div class="span6 responsive" data-tablet="span12 fix-offset" data-desktop="span6" style="width:100%;">

                        <!-- BEGIN EXAMPLE TABLE PORTLET-->

                        <div class="portlet box grey">

                            <div class="portlet-title">

                                <div class="caption"><i class="icon-user"></i>采集源列表</div>

                                <div class="actions controls">

                                    <a href="#form_modal_tj"  data-toggle="modal" data-target="#form_modal_tj" class="btn blue"><i class="icon-pencil"></i> 添加</a>
                                    <!--<a href="/Index/Authority/add" class="btn blue"><i class="icon-pencil"></i> 添加</a>-->

                                </div>

                            </div>

                            <div class="portlet-body">

                                <table class="table table-striped table-bordered table-hover" id="current_table">

                                    <thead>

                                        <tr>
                                            <th>名称</th>
                                            <th>采集网站</th>
                                            <th>采集机器人</th>
                                            <th>采集地址</th>
                                            <th>采集类别</th>
                                            <th>描述</th>
                                            <th>添加时间</th>
                                            <th>操作</th>
                                        </tr>

                                    </thead>

                                    <tbody id="tbody">
<?php
    foreach ($source_list as $k => $v) {
        $style  = (0 == ($k % 2)) ? 'odd' : 'even';
        $create_time = date('Y-m-d H:i:s', (int) $v['create_time']);
        $name    =  mb_substr($v['name'],0,20,'utf-8');
        $remark  =  mb_substr($v['remark'],0,20,'utf-8');
        echo <<< EOF
                                        <tr class="{$style} gradeX">

                                            <td>{$name}</td>
                                            <td>{$v['website_name']}</td>
                                            <td>{$v['robot_name']}</td>
                                            <td>{$v['url']}</td>
                                            <td>{$v['typical_name']}</td>
                                            <td>{$remark}</td>
                                            <td>{$create_time}</td>
                                            <td item_id="{$v['source_id']}" article_type_id="{$v['article_type_id']}" status="{$v['status']}" robot_id="{$v['robot_id']}" site_id="{$v['site_id']}" ><a class="tableDelete" href="/Source/del/id/{$v['source_id']}">删除</a> / <a  class="tableEdit" href="/Source/edit/id/{$v['source_id']}">修改</a></td>
                                        </tr>
EOF;
    }
?>
                                    </tbody>

                </table>
<div id="pages">
<?php
    if($source_list[0]['rows']/$source_list[0]['every_page']<=1 || empty($source_list))
        {
            
        }
        else
        {
            print_r($allpage);
        }
?>
</div>

                            </div>

                        </div>
                        
                                <div id="form_modal_tj" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">

                                    <div class="modal-header">

                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>

                                        <h3 id="myModalLabel3">添加采集源</h3>

                                    </div>

                                    <div class="modal-body">

                                        <form action="#" id="form_add" method="post" class="form-horizontal">

                                            <div class="control-group">

                                                <label class="control-label">名称</label>

                                                <div class="controls">

                                                    <input type="text" class="colorpicker-default m-wrap" name="name" value="">

                                                </div>

                                            </div>
                                            <div class="control-group">

                                                <label class="control-label">采集类别</label>

                                                <div class="controls">
                            <select name="article_type_id" id="article_typical" class="article_typical">
<?php
   /* foreach ($article_type_list AS $k => $v) {
        echo '<option value="' . $v['article_type_id'] . '">' . $v['name'] . '</option>';
    }*/
?>
                            </select>
                                                </div>

                                            </div>

                                            <div class="control-group">

                                                <label class="control-label">采集网站</label>

                                                <div class="controls">

                                                    <!-- <input type="text" class="colorpicker-default m-wrap" name="source" value=""> -->
                                                    <select class="colorpicker-default m-wrap" name="source" id="source" class="source_class">
                                                        <option value="--请选择采集的网站--">--请选择采集的网站--</option>	
                                                    </select>

                                                </div>

                                            </div>
                                            <div class="control-group">

                                                <label class="control-label">采集机器人</label>


                                                <div class="controls">
                                                    <select class="colorpicker-default m-wrap" name="coll_robot"
                                                    id="coll_robot" class="coll_robot">

                                                    </select>
                                                </div>

                                            </div>
                                            <div class="control-group">

                                                <label class="control-label">采集地址</label>

                                                <div class="controls">
                                                    <input type="text" class="colorpicker-default m-wrap" name="coll_address" value="" id="coll_address"  onchange="CheckUrl()">

                                                     <script type="text/javascript">
                                                    $(document).ready(function(){
      var txtUrl= $("#coll_address").val();
        
      $("#coll_address").on("blur",function(){
        var txtUrl= $("#coll_address").val();
        var reg =  /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/;
          if(!reg.test(txtUrl)){
            layer.alert("请输入正确URL");
            return false;
          }/*else{
            alert("ok");
            return true;*/
         
        });
        
      });
                                                    </script>

                                                </div>

                                            </div>


                                            
                                             <div class="control-group">

                                                <label class="control-label">描述</label>

                                                <div class="controls">

                        <textarea name="remark" class="colorpicker-default m-wrap"></textarea>

                                                </div>
                                            </div> 
                                                                              

                                        </form>

                                    </div>

                                    <div class="modal-footer">

                                        <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>

                                        <button class="btn green btn-primary" id="submit_form" data-dismiss="modal" >提交</button>
                                        
                                    </div>

                                </div>
                                
                                <div id="form_edit" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">

                                    <div class="modal-header">

                                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>

                                        <h3 id="myModalLabel3">修改采集源</h3>

                                    </div>

                                    <div class="modal-body">

                                        <form action="#" id="form_edit" method="post" class="form-horizontal">


                                            <div class="control-group">

                                                <label class="control-label">名称</label>

                                                <div class="controls">

                                                    <input type="text" class="colorpicker-default m-wrap" name="name" value="">

                                                </div>

                                            </div>
                                            <div class="control-group">

                                                <label class="control-label">采集类别</label>

                                                <div class="controls">
                            <select id="update_article_typical" name="article_type_id">
<?php
   /* foreach ($article_type_list AS $k => $v) {
        echo '<option value="' . $v['article_type_id'] . '">' . $v['name'] . '</option>';
    }*/
?>
                            </select>
                                                </div>
                                            </div>


                                            <div class="control-group">

                                                <label class="control-label">采集网站</label>

                                                

                                            <div class="controls">
                            <select id="update_source" name="source">
<?php
    foreach ($coll_site_list AS $k => $v) {
        echo '<option value="' . $v['website_id'] . '">' . $v['remark'] . '</option>';
    }
?>
                            </select>
                                                </div>

                      </div>


                                            <div class="control-group">

                                                <label class="control-label">采集机器人</label>

                                                <div class="controls">
                            <select id="update_coll_robot" name="robot_id">
<?php
    foreach ($robots_list AS $k => $v) {
        echo '<option value="' . $v['robot_id'] . '">' . $v['robot'] . '</option>';
    }
?>
                            </select>
                                                </div>

                      </div>
                                            <div class="control-group">

                                                <label class="control-label">采集地址</label>

                                                <div class="controls">

                                                    <input type="text" class="colorpicker-default m-wrap" name="coll_address" value="" id="update_address"> 

                                                </div>

                                            </div>

                                            <script type="text/javascript">
                                                    $(document).ready(function(){
      var txtUrl= $("#update_address").val();
        
      $("#update_address").on("blur",function(){
        var txtUrl= $("#update_address").val();
        var reg =  /^((https|http|ftp|rtsp|mms)?:\/\/)[^\s]+/;
          if(!reg.test(txtUrl)){
            layer.alert("请输入正确URL");
            return false;
          }/*else{
            alert("ok");
            return true;*/
         
        });
        
      });
                                                    </script>


                                            
                        
                      <div class="control-group">

                                                <label class="control-label">描述</label>

                                                <div class="controls">

                        <textarea name="remark" class="colorpicker-default m-wrap"></textarea>

                                                </div>

                                            </div>


                                        </form>

                                    </div>

                                    <div class="modal-footer">

                                        <button class="btn" data-dismiss="modal" aria-hidden="true">关闭</button>

                                        <button class="btn green btn-primary" id="edit_form" data-dismiss="modal" >提交</button>
                                        
                                    </div>

                                </div>

                        <!-- END EXAMPLE TABLE PORTLET-->

                    </div>

                </div>

                <!-- END PAGE CONTENT-->

            </div>

            <!-- END PAGE CONTAINER-->

        </div>

    <!-- END PAGE -->
<?php
    include APPLICATION_PATH . '/application/views/foot.phtml';
?>
